const app = getApp()
Page({
	data: {
		name:'金佳鑫',
		sex:'男',
		phone:13888705231,
		baseUrl: app.globalData.baseUrl,
		indexData:[]
	},
	onShow(options) {
		this.ready()
	},
	ready(){
		app.indexApi.myCenter(wx.getStorageSync('user').id).then(a => {
			if (a.code == 0) {
			if (!a.data.avatar) {
				a.data.avatar = '/upfiles/static/user_default.png'
			}
			var positionList=[{id:1,name:'总经办'},{id:2,name:'州三级经理'},{id:3,name:'县市三级经理'},{id:4,name:'县市主管'},{id:5,name:'客户经理'},{id:6,name:'行业经理'}]
			positionList.forEach(item => {
				if(item.id==a.data.position){
					a.data.position=item.name
				}
			  });
			  a.data.sex=='0'?a.data.sex=='女':a.data.sex=='男'
			  this.setData({
				indexData:a.data
			  })
			}
		})
	},
	toback(){
		wx.showModal({
		  title: '是否退出登录', //提示的标题
		  success: function(res) {
			if(res.confirm) {
			  wx.clearStorageSync('user')
			  wx.clearStorageSync('token')
			  wx.redirectTo({
				url: '/pages/login/index',
			  })
			}
		  }
		})
	  },
	changeHead(){
		wx.showActionSheet({
			itemList:['上传图片'], //文字数组
			success: (res) => {
			  var _this=this
			  wx.chooseImage({
		        complete: function (c) {
		          if (c.errMsg == 'chooseImage:ok') {
		            c.tempFilePaths.forEach((path) => {
		              wx.uploadFile({
		                url: _this.data.baseUrl + '/admin/picture/weChatUpload',
		                filePath: path,
		                name: 'file',
						header:{"Authorization": wx.getStorageSync('token')},
		                formData: {type:'head'},
		                success: function (u) {
							var data = JSON.parse(u.data);
							_this.data.indexData.avatar=data.data.urlAddress
							_this.setData({
								indexData:_this.data.indexData
							})
							app.indexApi.myCenterChange({
								id:wx.getStorageSync('user').id,
								avatar: data.data.urlAddress
							}).then(a => {
								if (a.code == 0) {
									wx.showToast({
									title: '修改成功',
									icon: 'none'
									})
								}
							})
						  
		                }
		              })
		            })
		          }
		        }
		      })
			},
		  })
	},
	changename(){
		wx.showModal({
			editable:true,//显示输入框
			placeholderText:'输入姓名',//显示输入框提示信息
			success: res => {
				if (res.confirm) { //点击了确认
				this.data.indexData.nickname=res.content
				this.setData({
					indexData:this.data.indexData
				})
				app.indexApi.myCenterChange({
					id:wx.getStorageSync('user').id,
					nickname:res.content
				}).then(a => {
					if (a.code == 0) {
						wx.showToast({
							title: '修改成功',
							icon: 'none'
						  })
					}
				})
			   } 
			}
		})
	},
	changesex(){
		var list=['男','女']
		wx.showActionSheet({
			itemList:list, //文字数组
			success: (res) => {
				this.data.indexData.sex=list[res.tapIndex]=='男'?1:0
				console.log(this.data.indexData.sex)
				this.setData({
					indexData:this.data.indexData
				})
				app.indexApi.myCenterChange({
					id:wx.getStorageSync('user').id,
					sex:list[res.tapIndex]=='男'?1:0,
				}).then(a => {
					if (a.code == 0) {
						wx.showToast({
						title: '修改成功',
						icon: 'none'
						})
					}
				})
			}
		})
	},
	changetel(){
		wx.showModal({
			editable:true,//显示输入框
			placeholderText:'输入手机号',//显示输入框提示信息
			success: res => {
				if (res.confirm) { //点击了确认
					this.data.indexData.mobile=res.content
					this.setData({
						indexData:this.data.indexData
					})
					app.indexApi.myCenterChange({
						id:wx.getStorageSync('user').id,
						mobile:res.content
					}).then(a => {
						if (a.code == 0) {
							wx.showToast({
								title: '修改成功',
								icon: 'none'
							  })
						}
					})
				} 
			}
		})
	},
})